package com.nega.modules.handler;

import com.nega.commons.model.SearchResult;
import com.nega.commons.spider.IPSourcePool;
import com.nega.commons.spider.SpiderCoreService;
import com.nega.modules.service.SpiderService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Garcia
 * @CreateDate: 2019/5/8 12:38
 * @Description: 微博扫描
 */

@Service
@Slf4j
public class SpiderWeiBoServiceImpl implements SpiderService {

    @Resource
    private SpiderCoreService spiderCoreService;

    @Resource
    private IPSourcePool ipSourcePool;

    private static final String DOMAIN  = "https://s.weibo.com";
    private static final String CONTEXT = "/article";

    @Override
    public List<SearchResult> getResultInfo(String text) throws IOException {

        log.info("start 微博数据爬取>>>");

        String ip = ipSourcePool.getIp();
        Map<String, String> params = getParams(text);
        long start = System.currentTimeMillis();
        Document document = spiderCoreService.getDocument(DOMAIN + CONTEXT, params, ip);
        List<SearchResult> results =new ArrayList<>();
        results.addAll(getInfo(document,text));
        log.info("数据提取结束，微博数量:{}，微博耗时:{}",results.size(), (System.currentTimeMillis() - start));
        return results;
    }

    private Map<String, String> getParams(String text) {
        Map<String, String> params = new HashMap<>();
        params.put("q", text);
        return params;
    }

    private List<SearchResult> getInfo(Document document,String text) {
        List<SearchResult> results = new ArrayList<>();
        if (document == null) {
            return results;
        }
        Element element = document.getElementById("pl_feedlist_index");
        Elements elements = element.getElementsByAttributeValue("class", "card-wrap");
        for (Element ele : elements) {
            SearchResult result = new SearchResult();
            try {
                result.setKeyWord(text);
                Element ap = ele.getElementsByAttribute("title").get(0);
                String title = ap.attr("title");
                String url = ap.attr("href");
                result.setTitle(title);
                result.setUrl(url);
                Elements spans = ele.getElementsByAttributeValue("class", "act").get(0).getElementsByTag("span");
                for (Element span : spans) {
                    if (span.hasAttr("node-type")) {
                        continue;
                    }
                    Elements as = span.getElementsByTag("a");
                    if (as != null && as.size() > 0) {
                        for (Element a : as) {
                            if (!a.hasAttr("title")) {
                                String source = a.text();
                                String sourceUrl = a.attr("href");
                                result.setSource(source);
                            }
                        }
                        continue;
                    }
                    String spanstr = span.text();
                    if (spanstr.contains("年") || spanstr.contains("日") || spanstr.contains("今天")) {
                        result.setTime(spanstr);
                    } else {
                        result.setSource(spanstr);
                    }
                }
                results.add(result);
            } catch (Exception e) {
                log.error("微博抓取页异常", e);
            }
        }
        return results;
    }
}
